<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>枫叶商城 - 登录页面</title>
    <link rel="stylesheet" href="../plugin/bootstrap/css/bootstrap.min.css" />
    <script src="../plugin/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../iconfont/iconfont.css" />
    <link rel="shortcut icon" href="../images/logo.ico" />
    <script src="../plugin/jQuery/jquery-3.6.0.js"></script>
    <script src="../plugin/axios.min.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      body,
      html {
        width: 100%;
        height: 100%;
        overflow: hidden;
        background: url("../images/background-img.jpg");
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }

      a {
        text-decoration: none;
        color: red;
      }

      .login {
        width: 350px;
        min-height: 350px;
        padding: 25px;
        background-color: whitesmoke;
        border: 2px solid black;
        border-radius: 5px;
        float: right;
        margin-right: 10%;
        margin-top: 14%;
      }

      .iconfont {
        font-size: 20px;
      }

      .login h2 {
        color: red;
        text-align: center;
        font-weight: 700;
      }

      .login span {
        font-size: 10px;
        font-weight: 700;
        color: gray;
      }

      .icon-next {
        font-size: 10px;
        font-weight: 700;
      }

      .back {
        position: absolute;
        top: 20px;
        left: 25px;
        width: 100px;
        height: 100px;
      }

      .yanzhen a,
      span {
        display: inline-block;
      }
    </style>
    <script>
      window.onload = function () {
        var login = document.getElementById("login");
        var phone = document.getElementById("data_name").value;
        var password = document.getElementById("data_password").value;
        var checkcode = document.getElementById("data_checkcode").value;
        // login.onclick = function() {
        //     var pattern = /^(13|14|15|17|18)[0-9]{9}$/;
        //     if (pattern.test(phone)) {
        //         alert("合法"); //??后期需要修改
        //     } else {
        //         alert("请输入正确的电话号码！");
        //     }
        // }
        //图片点击事件
        function changeCheckCode(img) {
          img.src = "/common/checkCode?" + new Date().getTime();
        }

        login.addEventListener("click", function () {
          var phone = document.getElementById("data_name").value;
          var password = document.getElementById("data_password").value;
          var checkcode = document.getElementById("data_checkcode").value;
          axios({
            method: "POST",
            url: "http://localhost:8080/user/login",
            data: {
              phone: phone,
              password: password,
              checkcode: checkcode,
            },
          }).then(function (res) {
            if (res.data.code === 1) {
              console.log(res.data);
              sessionStorage.setItem("user_id", res.data.data);
              sessionStorage.setItem("user_phone", phone);
              window.location.href = "../index.html";
            } else {
              alert("抱歉，登录失败！");
            }
          });
        });
      };
    </script>
  </head>

  <body>
    <div class="container-fluid m-0 p-0">
      <div class="main">
        <a href="../index.html">
          <img src="../images/返回.png" alt="" class="back" />
        </a>
        <div class="login">
          <h2>账户登录</h2>
          <div>
            <form action="" id="login_form">
              <div class="input-group mb-3">
                <span
                  class="input-group-text iconfont icon-nickname"
                  id="basic-addon1"
                ></span>
                <input
                  id="data_name"
                  type="text"
                  class="form-control"
                  placeholder="登录手机"
                  aria-label="登录手机"
                  name="name"
                />
              </div>
              <div class="input-group mb-3">
                <span
                  class="input-group-text iconfont icon-locking"
                  id="basic-addon2"
                ></span>
                <input
                  id="data_password"
                  type="password"
                  class="form-control"
                  placeholder="密码"
                  aria-label="密码"
                  name="password"
                />
              </div>
              <div class="yanzhen">
                <a href=""
                  ><img
                    src="/common/checkCode"
                    onclick="changeCheckCode(this)"
                    style="width: 80px; height: 30px; border: 2px solid black"
                    title="看不清点击刷新"
                    id="vcode"
                /></a>
                <span
                  ><input
                    id="data_checkcode"
                    type="text"
                    class="form-control"
                    placeholder="请输入验证码"
                    name="checkcode"
                /></span>
              </div>

              <span class="d-flex justify-content-end">忘记密码</span>
              <a
                id="login"
                href="#"
                class="btn bg-danger text-white d-flex justify-content-center my-3"
                >登录</a
              >
              <span
                class="iconfont icon-next d-flex justify-content-end text-danger"
                ><a href="register.html">立即注册</a></span
              >
            </form>
          </div>
        </div>
      </div>
    </div>
  </body>
</html>
